/*
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "customprogresswidget.h"
#include <QPaintEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>

CustomProgressWidget::CustomProgressWidget(int UID, QObject* receiver, QWidget* parent):QWidget(parent), m_Point(0,0), m_Value(0), m_UID(UID)
{
    connect( this, SIGNAL(setTaskProgress(int,int)), receiver, SLOT(setTaskProgress(int,int)));
}

CustomProgressWidget::~CustomProgressWidget()
{   
}

void CustomProgressWidget::mousePressEvent ( QMouseEvent * event )
{
    qDebug() << Q_FUNC_INFO;
    m_Point = event->pos();
    calculateValue();
    emit(setTaskProgress(m_UID, m_Value));
}

void CustomProgressWidget::resizeEvent ( QResizeEvent * event )
{
    if( m_Value != 0)
    {
        m_Point.setX( (event->size().width() * m_Value) / 100 );
        repaint();
    }
}

void CustomProgressWidget::calculateValue()
{
    m_Value = m_Point.x() *100 / (float)rect().width();
    if( m_Value >= 97 )
        m_Value  = 100;        
    repaint();
}

void CustomProgressWidget::paintEvent ( QPaintEvent * event )
{
    QPainter painter(this);
    painter.setPen(Qt::black);
    painter.fillRect(2,2,m_Point.x()-4,rect().height()-4, QBrush(QColor(Qt::gray)));
    painter.drawText(event->rect(),Qt::AlignHCenter | Qt::AlignVCenter, QString::number(m_Value));
}

void CustomProgressWidget::setValue(int value)
{
    qDebug() << Q_FUNC_INFO << value;
    m_Value = value;
    m_Point.setX( (m_Value * rect().width()) / 100);
}
